import { queryPage, queryDetail, add, edit, bathDelete } from '../services/${className}Service';
import { message } from 'antd';

export default {
    namespace: '${classObjectName}',
    state: {
        record:Object,
        page:{
            pageData:[],
            pageSize:10,
            pageNum:1,
            total:0
        },
        loading: true,
        editModalVisible:false
    },

    reducers: {
        pages(state, action) {
            return {
                ...state,
                page: action.payload,
            };
        },
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
        setDetail(state,{payload:{record}}){
            return {...state,record:record}
        },
        handleEditModalVisible(state,{payload:{visible}}){
            return {...state,editModalVisible:visible}
        }
    },
    effects: {
        *queryPages({ payload:{ pageNum,pageSize } }, { call, put }) {
            yield put({type: 'changeLoading', payload: true});
            const response = yield call(queryPage, {pageNum,pageSize});
            yield put({
                type: 'pages',
                payload: response,
            });
            yield put({type: 'changeLoading', payload: false});
        },
        *add({payload:{values},callback},{call,put,select}){
            try{
                yield put({type: 'changeLoading', payload: true});
                yield call(add,values);
                message.success('提交成功！');
                if(callback){
                    callback();
                }
                const {pageNum,pageSize} = yield select(state => state.${classObjectName}.page); // 从state获取原来的分页数据
                yield put({ type: 'queryPages', payload: { pageNum,pageSize} });
            }catch (e){
                message.error(e.message);
            }
            yield put({type: 'changeLoading', payload: false});
        },
        *edit({payload:{values},callback},{call,put,select}){
            try{
                yield put({type: 'changeLoading', payload: true});
                yield call(edit,values);
                if(callback){
                    callback();
                }
                message.success('编辑成功！');
                const {pageNum,pageSize} = yield select(state => state.${classObjectName}.page); // 从state获取原来的分页数据
                yield put({ type: 'queryPages', payload: { pageNum,pageSize} });
            }catch (e){
                message.error(e.message);
            }
            yield put({type: 'changeLoading', payload: false});
        },
        *detail({ payload: {id}}, { call, put }){
            yield put({type: 'changeLoading', payload: true});
            const record = yield call(queryDetail,{id:id});
            if(record){
                yield put({ type: 'setDetail', payload: { record} });
            }
            yield put({type: 'changeLoading', payload: false});
        },
        *delete({payload:{selectedRowKeys},callback},{call,put,select}){
            try{
                yield put({type: 'changeLoading', payload: true});
                yield call(bathDelete,{ids:selectedRowKeys});
                if(callback)
                    callback();
                message.success('删除成功！');
                const {pageNum,pageSize} = yield select(state => state.${classObjectName}.page); // 从state获取原来的分页数据
                yield put({ type: 'queryPages', payload: { pageNum,pageSize} });
            }catch (e){
                message.error(e.message);
            }
            yield put({type: 'changeLoading', payload: false});
        }
    }
};
